import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rider/main/page/main_page.dart';
import 'package:flutter_rider/module/login/page/login_page.dart';
import 'package:flutter_rider/module/mine/page/operator_manage_page.dart';
import 'package:flutter_rider/module/task/task_router.dart';
import 'package:flutter_rider/module/waybill/waybill_router.dart';
import 'package:flutter_rider/routers/i_router.dart';
import 'package:flutter_rider/routers/not_found_page.dart';
import 'package:flutter_rider/module/mine/mine_router.dart';
// import 'package:flutter_rider/ui/page/main_page.dart';
import 'package:flutter_rider/main/page/webview_page.dart';

class Routes {
  static String homePage = '/home';
  static String webViewPage = '/webView';
  static String loginPage = '/login';

  static final List<IRouterProvider> _listRouter = [];

  static final FluroRouter router = FluroRouter();

  static void initRoutes() {
    /// 指定路由跳转错误返回页
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      debugPrint('未找到目标页');
      return const NotFoundPage();
    });

    router.define(homePage,
        handler: Handler(
            handlerFunc:
                (BuildContext? context, Map<String, List<String>> params) =>
                    MainPage()));

    router.define(loginPage,
        handler: Handler(handlerFunc: (_, __) => LoginPage()));

    router.define(webViewPage, handler: Handler(handlerFunc: (_, params) {
      final String title = params['title']?.first ?? '';
      final String url = params['url']?.first ?? '';
      return WebViewPage(title: title, url: url);
    }));

    _listRouter.clear();

    /// 各自路由由各自模块管理，统一在此添加初始化
    _listRouter.add(MineRouter());
    _listRouter.add(TaskRouter());
    _listRouter.add(WaybillRouter());

    /// 初始化路由
    void initRouter(IRouterProvider routerProvider) {
      routerProvider.initRouter(router);
    }

    _listRouter.forEach(initRouter);
  }
}
